package task.manager;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.io.StreamCorruptedException;
import java.util.HashMap;
import java.util.Map;

import android.os.Environment;
import android.text.Editable;
import android.util.Log;

/**
 * Facade for login screen.
 * 
 * @author Harrison Saylor, Aaron Mead, Marc Huet, Greg Ruskin
 * @version 2
 */

public class LoginFacade {

	private static LoginFacade INSTANCE;
	private Map<String, String> loginData;
	
	public static LoginFacade getInstance(){
		
		if(INSTANCE == null){
			INSTANCE = new LoginFacade();
		}
		
		return INSTANCE;
	}
	
	public LoginFacade(){
		
		try {
			loadLoginData();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
		if(loginData == null){
			loginData = new LoginDataMap();
		} // End of if statement.
		
	}

	@SuppressWarnings("unchecked")
	private void loadLoginData() throws IOException {
		System.out.println("Loading login data....");
		ObjectInputStream ois = null;
		try {
			//we are using external storage, so we need to get the location
			//be sure you have the correct privileges 
			File file = new File(Environment.getExternalStorageDirectory() + "/login.bin");
			ois = new ObjectInputStream(new FileInputStream(file));
			loginData = (Map<String, String>)ois.readObject();
		} catch (StreamCorruptedException e) {
			Log.e("APPLICATION FACADE", "Corrupted binary file");
		} catch (FileNotFoundException e) {
			Log.e("APPLICATION FACADE", "Binary File not found");
			System.out.println("Binary File not found");
		} catch (IOException e) {
			Log.e("APPLICATION FACADE", "General IO Error");
		} catch (ClassNotFoundException e) {
			Log.e("APPLICATION FACADE", "Class not found");
		} finally {
			if (ois != null)
				ois.close();
		} // End of try/catch/finally statements.
		
	} // End of loadUsers method.
	
	public void saveLoginData() throws IOException {
		System.out.println("Saving login data....");
		ObjectOutputStream oos = null;
		try {
			File file = new File(Environment.getExternalStorageDirectory() + "/login.bin");
			//System.out.println("File created");
			oos = new ObjectOutputStream(new FileOutputStream(file));
			//System.out.println("ObjectOutputStream instantiated");
			oos.writeObject(loginData);
			//System.out.println("users object written to file");
			oos.flush();
			//System.out.println("objectOutputStream flushed");
		} catch (IOException e) {
			Log.e("TaskManagerActivity", "Binary file input general error");
			e.printStackTrace();
		} finally {
			if (oos != null) {
				oos.close();
			} // End of if statement.
		} // End of try/catch/finally statements.
	} // End of saveUsers method.

	public void put(String text, String text2) {
		loginData.put(text, text2);
		System.out.println("Entry entered");
		//File file = new File(Environment.getExternalStorageDirectory() + "/" + text);
		
	}

	public boolean isValid(String username, String password) {
		return   ((LoginDataMap)loginData).isValid(username,password);
	}
	
}


